﻿using Git.Storage.Common.Result;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;

namespace Git.Storage.WebApi.Utility.Filters
{
    /// <summary>
    ///  异常过滤器  
    /// </summary>
    public class CustomExceptionAsyncFilterAttribute : Attribute, IAsyncExceptionFilter
    {
        /// <summary>
        /// 异常捕获方法
        /// 如果这么写不建议标记全局，如果返回图片/文件流  不建议这么返回
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task OnExceptionAsync(ExceptionContext context)
        {
            if (context.ExceptionHandled == false)
            {
                context.Result = new JsonResult(new ApiResult()
                {
                    Success = false,
                    Message = context.Exception.Message,
                });
                context.ExceptionHandled = true;
            }

            return Task.CompletedTask;
        }
    }
}
